/**
 * 作者: wgx
 * 日期: 2024-11-26 17:10:20
 * 备注: 用户商店限购信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.commondata.shop.Shop;
import com.motu.monstercity.server.game.commondata.shop.ShopConstant;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;

public class UserShopLimit extends UserShopLimitModel {
    public UserShopLimit() {
        super();
    }

    public void reset() {
        long curTime = TimeUtils.getCurTime();
        boolean isUpdate = false;
        if (!TimeUtils.isToday(getDailyTime())) {// 重置每日限购的数据
            putDailyBuy("");
            putDailyTime(curTime);
            isUpdate = true;
        }
        if (!TimeUtils.isSameWeek(curTime, getWeekTime())) {// 重置每周限购的数据
            putWeekBuy("");
            putWeekTime(curTime);
            isUpdate = true;
        }
        if (!TimeUtils.isSameMonth(getMonthTime())) {// 重置每月限购的数据
            putMonthBuy("");
            putMonthTime(curTime);
            isUpdate = true;
        }
        if (isUpdate) {
            update();
        }
    }

    public String takeBuyStr(Shop shop) {
        String buyStr = "";
        if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_1) {//  每日限购
            buyStr = getDailyBuy();
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_2) {// 每周限购
            buyStr = getWeekBuy();
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_3) {// 每月限购
            buyStr = getMonthBuy();
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_4) {// 终身限购
            buyStr = getTotalBuy();
        }
        if (Tool.isEmpty(buyStr)) {
            return new JsonObject().toString();// jsonobject格式的字符串
        } else {
            return buyStr;
        }
    }

    public int takeBuyNum(Shop shop) {
        String buyStr = takeBuyStr(shop);
        if (Tool.isEmpty(buyStr)) {
            return 0;// 无购买记录
        }
        JsonObject jsonObject = new JsonObject(buyStr);
        String key = shop.getId() + "";// shopid 当做key
        if (jsonObject.containsKey(key)) {
            return jsonObject.getInteger(key);// 无购买记录
        } else {
            return 0;
        }
    }
}
